import { request } from '@/utils/request'
import type { SysRole } from '@/types/role'
import type { PageQuery } from '@/types/api'

// 获取角色列表
export function getRoleList(params?: Record<string, any>) {
  return request<SysRole[]>({
    url: '/role/list',
    method: 'get',
    params
  })
}

// 分页获取角色列表
export function getRolePage(params: PageQuery & Record<string, any>) {
  return request<{
    rows: SysRole[]
    total: number
  }>({
    url: '/role/page',
    method: 'get',
    params
  })
}

// 获取角色详情
export function getRoleDetail(roleId: number) {
  return request<SysRole>({
    url: `/role/${roleId}`,
    method: 'get'
  })
}

// 新增角色
export function addRole(data: Partial<SysRole>) {
  return request({
    url: '/role',
    method: 'post',
    data
  })
}

// 修改角色
export function updateRole(data: Partial<SysRole>) {
  return request({
    url: '/role',
    method: 'put',
    data
  })
}

// 删除角色
export function deleteRole(roleId: number) {
  return request({
    url: `/role/${roleId}`,
    method: 'delete'
  })
}

// 获取角色拥有的权限
export function getRolePermissions(roleId: number) {
  return request({
    url: `/role/permissions/${roleId}`,
    method: 'get'
  })
}

// 分配角色权限
// permissionIds 可以是字符串数组或数字数组，后端会自动转换
export function assignPermissions(roleId: number, permissionIds: (string | number)[]) {
  return request({
    url: `/role/permissions/${roleId}`,
    method: 'put',
    data: permissionIds
  })
}
